home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / CopyBits vs. CopyMask / Source / Test.c < prev   
Encoding:
Text File  |  1996-09-17  |  10.8 KB  |  454 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. //    Test.c
  3. //
  4. //    By: Tony Myles
  5. //
  6. // Copyright © 1992-93 Apple Computer Inc., All rights reserved.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <QuickDraw.h>
  11. //#include <DebugUtils.h>
  12. #include <Dialogs.h>
  13. //#include <GWorldUtils.h>
  14. #include <TextUtils.h>
  15. #include <Resources.h>
  16. #include "Sample.h"
  17. #include "Test.h"
  18.  
  19.  
  20. ///--------------------------------------------------------------------------------------
  21. //    LetTheGameBegin
  22. ///--------------------------------------------------------------------------------------
  23.  
  24. void LetTheGameBegin(DialogPtr srcDialogP)
  25. {
  26.     OSErr err;
  27.     CGrafPtr saveCPort;
  28.     GDHandle saveGDevice;
  29.     PixMapHandle srcPixMap;
  30.     GWorldPtr pictGWorld = NULL, maskGWorld = NULL;
  31.     GrafPtr offScrnPort = NULL;
  32.     RgnHandle maskRgn = NULL;
  33.     Rect srcRect, dstRect, itemRect;
  34.     short frame;
  35.     long ticks;
  36.     Str255 itemStr;
  37.     PixPatHandle pixPatH;
  38.  
  39.     SetCursor(*GetCursor(watchCursor));
  40.  
  41.     GetGWorld(&saveCPort, &saveGDevice);
  42.     SetGWorld((CGrafPtr)srcDialogP, NULL);
  43.  
  44.     itemStr[0] = 0;
  45.     SetDItemText(srcDialogP, kCopyBitsFramesItem, itemStr);
  46.     SetDItemText(srcDialogP, kCopyBitsTicksItem, itemStr);
  47.     SetDItemText(srcDialogP, kCopyBitsFPSItem, itemStr);
  48.     SetDItemText(srcDialogP, kCopyMaskFramesItem, itemStr);
  49.     SetDItemText(srcDialogP, kCopyMaskTicksItem, itemStr);
  50.     SetDItemText(srcDialogP, kCopyMaskFPSItem, itemStr);
  51.  
  52.     GetDItemRect(srcDialogP, kCopyBitsPictItem, &itemRect);
  53.     OffsetRect(&itemRect, -itemRect.left, -itemRect.top);
  54.     itemRect.right += kFrameCount;
  55.  
  56.     err = CreateOptimumGWorld(&pictGWorld, &itemRect);
  57.  
  58.     if (err == noErr)
  59.     {
  60.         SetGWorld(pictGWorld, NULL);
  61.         srcPixMap = GetGWorldPixMap(pictGWorld);
  62.         (void)LockPixels(srcPixMap);
  63.  
  64.         pixPatH = GetPixPat(kPixPatResID);
  65.  
  66.         if (pixPatH != NULL)
  67.         {
  68.             FillCRect(&itemRect, pixPatH);
  69.             DisposePixPat(pixPatH);
  70.         }
  71.  
  72.         err = CreateGWorldFromPictResource(&maskGWorld, kApplePictResID);
  73.     }
  74.  
  75.     if (err == noErr)
  76.     {
  77.         offScrnPort = NULL;
  78.         err = CreateGrafPortFromPictResource(kApplePictResID, &offScrnPort);
  79.     }
  80.  
  81.     if (err == noErr)
  82.     {
  83.         maskRgn = NewRgn();
  84.     
  85.         err = BitMapToRegion(maskRgn, &offScrnPort->portBits);
  86.     }
  87.  
  88.     if (err == noErr)
  89.     {
  90.         GetDItemRect(srcDialogP, kCopyBitsPictItem, &itemRect);
  91.         OffsetRgn(maskRgn, itemRect.left, itemRect.top);
  92.     
  93.         srcRect = itemRect;
  94.         OffsetRect(&srcRect, -srcRect.left, -srcRect.top);
  95.         dstRect = itemRect;
  96.     
  97.         SetGWorld((CGrafPtr)srcDialogP, NULL);
  98.  
  99.         ticks = TickCount();
  100.  
  101.         for (frame = 0; frame < kFrameCount; frame++)
  102.         {
  103.             CopyBits((BitMapPtr)*srcPixMap, &srcDialogP->portBits,
  104.                         &srcRect, &dstRect, srcCopy, maskRgn);
  105.     
  106.             srcRect.left++;
  107.             srcRect.right++;
  108.         }
  109.     
  110.         ticks = TickCount() - ticks;
  111.     
  112.         NumToString(kFrameCount, itemStr);
  113.         SetDItemText(srcDialogP, kCopyBitsFramesItem, itemStr);
  114.         NumToString(ticks, itemStr);
  115.         SetDItemText(srcDialogP, kCopyBitsTicksItem, itemStr);
  116.         NumToString((ticks < 60) ? kFrameCount : kFrameCount / (ticks / 60), itemStr);
  117.         SetDItemText(srcDialogP, kCopyBitsFPSItem, itemStr);
  118.     
  119.         GetDItemRect(srcDialogP, kCopyMaskPictItem, &itemRect);
  120.         srcRect = itemRect;
  121.         OffsetRect(&srcRect, -srcRect.left, -srcRect.top);
  122.         dstRect = itemRect;
  123.     
  124.         ticks = TickCount();
  125.     
  126.         for (frame = 0; frame < kFrameCount; frame++)
  127.         {
  128.             CopyMask((BitMapPtr)*srcPixMap, &offScrnPort->portBits, &srcDialogP->portBits,
  129.                         &srcRect, &offScrnPort->portRect, &dstRect);
  130.     
  131.             srcRect.left++;
  132.             srcRect.right++;
  133.         }
  134.     
  135.         ticks = TickCount() - ticks;
  136.     
  137.         NumToString(kFrameCount, itemStr);
  138.         SetDItemText(srcDialogP, kCopyMaskFramesItem, itemStr);
  139.         NumToString(ticks, itemStr);
  140.         SetDItemText(srcDialogP, kCopyMaskTicksItem, itemStr);
  141.         NumToString((ticks < 60) ? kFrameCount : kFrameCount / (ticks / 60), itemStr);
  142.         SetDItemText(srcDialogP, kCopyMaskFPSItem, itemStr);
  143.     
  144.         UnlockPixels(srcPixMap);
  145.     }
  146.  
  147.     if (maskRgn != NULL)
  148.         DisposeRgn(maskRgn);
  149.  
  150.     if (offScrnPort != NULL)
  151.         DisposeGrafPort(offScrnPort);
  152.  
  153.     if (maskGWorld != NULL)
  154.         DisposeGWorld(maskGWorld);
  155.  
  156.     if (pictGWorld != NULL)
  157.         DisposeGWorld(pictGWorld);
  158.  
  159.     SetCursor(&qd.arrow);
  160.  
  161.     SysBeep(0);
  162. }
  163.  
  164.  
  165. ///--------------------------------------------------------------------------------------
  166. // CreateOptimumGWorld
  167. //
  168. //    create a new GWorld optimized for speed in copying
  169. //    to the graphics device that intersects the given rect.
  170. ///--------------------------------------------------------------------------------------
  171.  
  172. OSErr CreateOptimumGWorld(GWorldPtr *optGWorld, Rect *devRect)
  173. {
  174.     OSErr err;
  175.     CGrafPtr saveCPort;
  176.     GDHandle saveGDevice;
  177.     GWorldPtr tempGWorld;
  178.     //PixMapHandle pixMapH;
  179.     Rect tempRect = *devRect;
  180.  
  181.     *optGWorld = NULL;
  182.  
  183.     GetGWorld(&saveCPort, &saveGDevice);
  184.  
  185.     LocalToGlobal((Point *)&tempRect.top);
  186.     LocalToGlobal((Point *)&tempRect.bottom);
  187.  
  188.     err = NewGWorld(&tempGWorld, 0, &tempRect, NULL, NULL, 0);
  189.  
  190.     if (err == noErr)
  191.     {
  192.         SetGWorld(tempGWorld, NULL);
  193.         EraseRect(&tempGWorld->portRect);
  194.  
  195.         *optGWorld = tempGWorld;
  196.     }
  197.  
  198.     SetGWorld(saveCPort, saveGDevice);
  199.  
  200.     return err;
  201. }
  202.  
  203.  
  204. ///--------------------------------------------------------------------------------------
  205. //    CreateGWorldFromPict
  206. //
  207. //    creates a offScreen GWorld and draws the specified pict into it
  208. ///--------------------------------------------------------------------------------------
  209.  
  210. OSErr CreateGWorldFromPict(GWorldPtr *pictGWorld, PicHandle pictH)
  211. {
  212.     OSErr err;
  213.     CGrafPtr saveCPort;
  214.     GDHandle saveGDevice;
  215.     GWorldPtr tempGWorld;
  216.     PixMapHandle tempPixMapH;
  217.     Rect pictRect;
  218.  
  219.     *pictGWorld = NULL;
  220.  
  221.     GetGWorld(&saveCPort, &saveGDevice);
  222.  
  223.     pictRect.left = pictRect.top = 0;
  224.     pictRect.right = (**pictH).picFrame.right - (**pictH).picFrame.left;
  225.     pictRect.bottom = (**pictH).picFrame.bottom - (**pictH).picFrame.top;
  226.  
  227.     err = CreateOptimumGWorld(&tempGWorld, &pictRect);
  228.  
  229.     if (err == noErr)
  230.     {
  231.         *pictGWorld = tempGWorld;
  232.  
  233.         SetGWorld(tempGWorld, NULL);
  234.  
  235.         tempPixMapH = GetGWorldPixMap(tempGWorld);
  236.  
  237.         (void)LockPixels(tempPixMapH);
  238.         DrawPicture(pictH, &pictRect);
  239.         UnlockPixels(tempPixMapH);
  240.     }
  241.  
  242.     SetGWorld(saveCPort, saveGDevice);
  243.  
  244.     return err;
  245. }
  246.  
  247.  
  248. ///--------------------------------------------------------------------------------------
  249. // CreateGWorldFromPictResource
  250. ///--------------------------------------------------------------------------------------
  251.  
  252. OSErr CreateGWorldFromPictResource(GWorldPtr *pictGWorldP, short pictResID)
  253. {
  254.     OSErr err;
  255.     PicHandle newPictH;
  256.  
  257.     newPictH = GetPicture(pictResID);
  258.  
  259.     if (newPictH != NULL)
  260.     {
  261.         err = CreateGWorldFromPict(pictGWorldP, newPictH);
  262.  
  263.         ReleaseResource((Handle)newPictH);
  264.     }
  265.  
  266.     return err;
  267. }
  268.  
  269.  
  270. ////--------------------------------------------------------------------------------------
  271. //    CreateGrafPort
  272. //
  273. // originally written by Forrest Tanaka
  274. ////--------------------------------------------------------------------------------------
  275.  
  276. OSErr CreateGrafPort(Rect *newPortRect, GrafPtr *newPort)
  277. {
  278.     OSErr err = noErr;
  279.     GrafPtr savePort;        // save port for later restore
  280.     GrafPtr localPort;    // local copy of GrafPort
  281.     Rect localPortRect;    // local copy of bounds
  282.  
  283.     *newPort = NULL;
  284.  
  285.         // save off the current port
  286.     GetPort(&savePort);
  287.  
  288.         // set the top-left corner of bounds to (0,0)
  289.     localPortRect = *newPortRect;
  290.     OffsetRect(&localPortRect, -newPortRect->left, -newPortRect->top);
  291.  
  292.         // allocate a new GrafPort
  293.     localPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
  294.  
  295.     if (localPort != NULL)
  296.     {
  297.             // initialize the new port and make the current port
  298.         OpenPort(localPort);
  299.         SetPort(localPort);
  300.         ForeColor(blackColor);
  301.         BackColor(whiteColor);
  302.  
  303.             // initialize and allocate the bitmap
  304.         localPort->portBits.bounds = localPortRect;
  305.           localPort->portBits.rowBytes = ((localPortRect.right + 15) >> 4) << 1;
  306.         localPort->portBits.baseAddr = NewPtrClear(localPort->portBits.rowBytes *
  307.                                                                                             (long)localPortRect.bottom);
  308.  
  309.         if (localPort->portBits.baseAddr != NULL)
  310.         {
  311.                 // clean up the new port
  312.             localPort->portRect = localPortRect;
  313.             ClipRect(&localPortRect);
  314.             RectRgn(localPort->visRgn, &localPortRect);
  315.             EraseRect(&localPortRect);
  316.  
  317.             *newPort = localPort;
  318.         }
  319.         else // allocation failed
  320.         {
  321.                 // capture the error
  322.             err = MemError();
  323.  
  324.                 // deallocate the port
  325.             ClosePort(localPort);
  326.             DisposePtr((Ptr)localPort);
  327.         }
  328.     }
  329.     else
  330.     {
  331.         err = MemError();
  332.     }
  333.  
  334.     SetPort(savePort);
  335.  
  336.     return err;
  337. }
  338.  
  339.  
  340. ////--------------------------------------------------------------------------------------
  341. //    CreateGrafPortFromPictResource
  342. //
  343. //    this routine will set up an offscreen port, and draw a pict into it
  344. //    the offscreen port is the exact size of the pict
  345. ///--------------------------------------------------------------------------------------
  346.  
  347. OSErr CreateGrafPortFromPictResource(short pictID, GrafPtr *offScrnPort)
  348. {
  349.     OSErr            err;
  350.     GrafPtr        savePort;
  351.     PicHandle    pictH;
  352.     Rect            tmpRect;
  353.  
  354.     GetPort(&savePort);
  355.  
  356.     pictH = GetPicture(pictID);
  357.  
  358.     if (pictH != NULL)
  359.     {
  360.         tmpRect.left = tmpRect.top = 0;
  361.         tmpRect.right = (**pictH).picFrame.right - (**pictH).picFrame.left;
  362.         tmpRect.bottom = (**pictH).picFrame.bottom - (**pictH).picFrame.top;
  363.  
  364.             //    create a port
  365.         err = CreateGrafPort(&tmpRect, offScrnPort);
  366.  
  367.         if (err == noErr)
  368.         {
  369.             SetPort(*offScrnPort);
  370.  
  371.                 //    draw the picture
  372.             DrawPicture(pictH, &tmpRect);
  373.         }
  374.  
  375.         ReleaseResource((Handle)pictH);
  376.     }
  377.     else
  378.     {
  379.         err = ResError();
  380.         
  381.         if (err == noErr)
  382.         {
  383.             err = resNotFound;
  384.         }
  385.     }
  386.  
  387.     SetPort(savePort);
  388.  
  389.     return err;
  390. }
  391.  
  392.  
  393. ////--------------------------------------------------------------------------------------
  394. //    DisposeGrafPort
  395. //
  396. // originally written by Forrest Tanaka
  397. ///--------------------------------------------------------------------------------------
  398.  
  399. void DisposeGrafPort(GrafPtr doomedPort)
  400. {
  401.     ClosePort(doomedPort);
  402.     DisposePtr(doomedPort->portBits.baseAddr);
  403.     DisposePtr((Ptr)doomedPort);
  404. }
  405.  
  406.  
  407. ///--------------------------------------------------------------------------------------
  408. // GetDItemText
  409. //
  410. //    get the text of a dialog item
  411. ///--------------------------------------------------------------------------------------
  412.  
  413. void GetDItemText(DialogPtr dlgP, short itemNum, Str255 iStr)
  414. {
  415.     short itemType;
  416.     Rect itemRect;
  417.     Handle itemH;
  418.  
  419.     GetDialogItem(dlgP, itemNum, &itemType, &itemH, &itemRect);
  420.     GetDialogItemText(itemH, iStr);                
  421. }
  422.  
  423.  
  424. ///--------------------------------------------------------------------------------------
  425. // SetDItemText
  426. //
  427. //    set the text of a dialog item
  428. ///--------------------------------------------------------------------------------------
  429.  
  430. void SetDItemText(DialogPtr dlgP, short itemNum, Str255 iStr)
  431. {
  432.     short itemType;
  433.     Rect itemRect;
  434.     Handle itemH;
  435.  
  436.     GetDialogItem(dlgP, itemNum, &itemType, &itemH, &itemRect);
  437.     SetDialogItemText(itemH, iStr);                
  438. }
  439.  
  440.  
  441. ///--------------------------------------------------------------------------------------
  442. // GetDItemRect
  443. //
  444. //    return the rect of a dialog item
  445. ///--------------------------------------------------------------------------------------
  446.  
  447. void GetDItemRect(DialogPtr dlgP, short itemNum, Rect *itemRect)
  448. {
  449.     short itemType;
  450.     Handle itemH;
  451.  
  452.     GetDialogItem(dlgP, itemNum, &itemType, &itemH, itemRect);
  453. }
  454.